﻿using MaplePacketLib2.Tools;
using MapleServer2.Constants;
using MapleServer2.Types;

namespace MapleServer2.Packets;

public static class ChatStickerPacket
{
    private enum Mode : byte
    {
        LoadChatSticker = 0x0,
        ExpiredStickerNotification = 0x1,
        AddSticker = 0x2,
        UseSticker = 0x3,
        GroupChatSticker = 0x4,
        Favorite = 0x5,
        Unfavorite = 0x6
    }

    public static PacketWriter LoadChatSticker(Player player)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.LoadChatSticker);
        pWriter.WriteShort((short) player.FavoriteStickers.Count);
        foreach (int favorite in player.FavoriteStickers)
        {
            pWriter.WriteInt(favorite);
        }
        pWriter.WriteShort((short) player.ChatSticker.Count);
        foreach (ChatSticker stickerGroup in player.ChatSticker)
        {
            pWriter.WriteInt(stickerGroup.GroupId);
            pWriter.WriteLong(TimeInfo.Now() + 2629743);
        }
        return pWriter;
    }

    public static PacketWriter ExpiredStickerNotification(List<ChatSticker> stickers)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.ExpiredStickerNotification);
        pWriter.WriteInt();
        pWriter.WriteInt(stickers.Count);
        foreach (ChatSticker sticker in stickers)
        {
            pWriter.WriteInt(sticker.GroupId);
        }
        return pWriter;
    }

    public static PacketWriter AddSticker(int itemId, int stickerGroupId, long expiration)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.AddSticker);
        pWriter.WriteInt(itemId);
        pWriter.WriteInt(1);
        pWriter.WriteInt(stickerGroupId);
        pWriter.WriteLong(expiration);
        return pWriter;
    }

    public static PacketWriter UseSticker(int stickerId, string script)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.UseSticker);
        pWriter.WriteInt(stickerId);
        pWriter.WriteUnicodeString(script);
        pWriter.WriteByte();
        return pWriter;
    }

    public static PacketWriter GroupChatSticker(int stickerId, string groupChatName)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.GroupChatSticker);
        pWriter.WriteInt(stickerId);
        pWriter.WriteUnicodeString(groupChatName);
        return pWriter;
    }

    public static PacketWriter Favorite(int stickerId)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.Favorite);
        pWriter.WriteInt(stickerId);
        return pWriter;
    }

    public static PacketWriter Unfavorite(int stickerId)
    {
        PacketWriter pWriter = PacketWriter.Of(SendOp.ChatSticker);
        pWriter.Write(Mode.Unfavorite);
        pWriter.WriteInt(stickerId);
        return pWriter;
    }
}
